package com.yisu.auction.api.core.service.impl.func;

import com.yisu.auction.api.core.mapper.BalanceLogMapper;
import com.yisu.auction.api.core.mapper.MemberMapper;
import com.yisu.auction.api.core.service.BaseService;
import com.yisu.auction.ewf.entity.BalanceLog;
import com.yisu.auction.ewf.entity.Member;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;


/**
 * author: 赵杰
 * Business Description：余额流水公用提取
 * createTime：2017/9/10
 */
public class BalanceLogUtil extends BaseService {
    @Resource
    private MemberMapper memberMapper;
    @Resource
    private BalanceLogMapper balanceLogMapper;

    /**
     * 添加会员钱包拍币
     *
     * @param operateNum  操作值
     * @param prizeId     竞品ID
     * @param walletType  钱包拍币类型 1 拍币 2赠币
     * @param operateDesc 操作类型
     * @return
     */
    public  boolean addMemberWallet(Member member,BigDecimal operateNum, long prizeId, Integer walletType, String operateDesc) {
        if(member.getType()==999)
            return true;
        BalanceLog balanceLog = new BalanceLog();
        Member updateMember = new Member();
        updateMember.setId(member.getId());
        updateMember.setUpdateDate(new Date());
        balanceLog.setMemberId(member.getId());
        balanceLog.setOperateNum(operateNum);
        balanceLog.setOperationType(BalanceLog.OperationTypeEnum.ADD.code);
        balanceLog.setOperationDesc(operateDesc);
        balanceLog.setCreateDate(new Date());
        balanceLog.setPrizeId(prizeId);
        if (walletType == BalanceLog.WalletTypeEnum.WALLET.code) {
            //充值拍币
            balanceLog.setBeforeWallet(member.getWallet());
            balanceLog.setAfterWallet(member.getWallet().add(balanceLog.getOperateNum()));
            balanceLog.setAfterPresentWallet(member.getPresentWallet());
            balanceLog.setBeforePresentWallet(member.getPresentWallet());
            balanceLog.setWalletType(BalanceLog.WalletTypeEnum.PRESENT_WALLET.code);
            updateMember.setWallet(member.getWallet().add(balanceLog.getOperateNum()));
        }
        if (walletType == BalanceLog.WalletTypeEnum.PRESENT_WALLET.code) {
            //充值赠币
            balanceLog.setBeforeWallet(member.getWallet());
            balanceLog.setAfterWallet(member.getWallet());
            balanceLog.setAfterPresentWallet(member.getPresentWallet().add(balanceLog.getOperateNum()));
            balanceLog.setBeforePresentWallet(member.getPresentWallet());
            balanceLog.setWalletType(BalanceLog.WalletTypeEnum.WALLET.code);
            updateMember.setPresentWallet(member.getPresentWallet().add(balanceLog.getOperateNum()));
        }
        balanceLogMapper.insertSelective(balanceLog);
        memberMapper.updateByPrimaryKeySelective(updateMember);
        return true;
    }

}
